package website;

import java.util.Vector;

import call4paper.*;
import exceptions.DoesNotExistCFPException;
import exceptions.ReviewerUserException;
import exceptions.AlreadyExistsCFPException;
import exceptions.AlreadyLoggedOnUserException;
import exceptions.InvalidUserException;

public class Website {

	//ACTORES
	private AdminWebsite admin = new AdminWebsite();
	private User loggedUser;
	
	
	public void userCreateAccount(String username, String password, 
								String firstname, String lastname, 
								String email, Vector<InterestArea> exp) {
		
		//verifica que user != loggedUser y lo setea.
		//this.setLoggedUser(User user)
		
		User user = new User(username, password, firstname, lastname, email, exp);
		
		try {
			if (this.validateUser(user)) {
				try {
					if(!this.alreadyLoggedOn(user)) {
						this.setLoggedUser(user);
					}
				} catch (AlreadyLoggedOnUserException e) {}
			}
		} catch (InvalidUserException e) {}

	}	
		
	private boolean alreadyLoggedOn(User user) throws AlreadyLoggedOnUserException {
		
		if(this.getLoggedUser().equals(user)) {
			return true;
		} else { throw new AlreadyLoggedOnUserException(); }
	}

	public void userLogOut() {
		
		this.loggedUser = null;
	}
	
	private boolean validateUser(User user) throws InvalidUserException {
		
		if(this.getAdmin().alreadyRegistered(user)) {
			return true;
		} else { throw new InvalidUserException(); }
	}


	public void userCreateCFP(User cfpCreator, String cfpName, 
								Vector<InterestArea> intAreas) {
		
		Vector<User> possibleReviewers = this.admin.showPossibleReviewers(intAreas);
		CFP newCFP = this.getLoggedUser().createCFP(cfpName, intAreas, possibleReviewers);
		if(!this.admin.getCfpsCreated().contains(newCFP))
			this.admin.getCfpsCreated().add(newCFP);
		else
			try {
				throw new AlreadyExistsCFPException();
			} catch (AlreadyExistsCFPException e) {}
	}
	
	public void userCreatePaperJob(User paperCreator, String title, String body, Vector<InterestArea> intAreas) {
		
		this.getLoggedUser().createPaperJob(title, body, intAreas);
	}
	
	public void userUploadPaperJob(PaperJob paper, String cfpName) {
		
		//TODO: excepcion cuando el user no es revisor de ese cfp.
		try {
		CFP cfp;
		try {
			cfp = this.getAdmin().searchCFP(cfpName);
			this.getLoggedUser().uploadPaperJob(paper, cfp);
		} catch (DoesNotExistCFPException e) {} 
		} catch (ReviewerUserException e) {}
	}
	
	public void userReviewPaperJob(PaperJob paper, Qualification quali) {
		
		this.getLoggedUser().evaluateJob(paper, quali);
	}
	
	public void userCheckAssignation(CFP aCFP){
		
		this.getLoggedUser().checkAssignation(aCFP);
	}
	
	//
	// GETTERS & SETTERS
	//
	public AdminWebsite getAdmin() {
		return admin;
	}

	public void setAdmin(AdminWebsite admin) {
		this.admin = admin;
	}
	
	public User getLoggedUser() {
		return loggedUser;
	}
	
	public void setLoggedUser(User user) {
		this.loggedUser = user;
	}

	
	
}
